package org.dashboard.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import org.dashboard.bean.AdRole;
import org.dashboard.bean.JsonResponse;
import org.dashboard.service.AdRoleService;
import org.dashboard.utils.NameCastTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/role")
public class AdRoleController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(AdRoleController.class);

    @Reference
    private AdRoleService adRoleService;

    @RequestMapping(value = "/search", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse search(HttpServletRequest request) {
        String criteria = request.getParameter("criteria");
        Integer pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));

        PageInfo pageInfo = adRoleService.selectAdRolesPaged(criteria, pageIndex, pageSize);

        JsonResponse jsonResponse = new JsonResponse();
        Map<String, Object> map = new HashMap<>();

        map.put("items", NameCastTools.listToListMap(pageInfo.getList()));
        map.put("count", pageInfo.getTotal());

        jsonResponse.setData(map);
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

    @RequestMapping(value = "/add", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse add(HttpServletRequest request) {
        String roleName = request.getParameter("roleName");
        Integer roleType = Integer.parseInt(request.getParameter("roleType"));

        AdRole adRole = new AdRole();
        adRole.setRoleName(roleName);
        adRole.setRoleType(roleType);

        adRole.setRoleId(adRoleService.insert(adRole));
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adRole));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/delete", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse delete(HttpServletRequest request) {
        String roleIds = request.getParameter("roleIds");
        JSONArray jsonArray = (JSONArray) JSON.parse(roleIds);
        ArrayList<Integer> ids = new ArrayList<>();
        if (jsonArray != null) {
            int len = jsonArray.size();
            for (int i = 0; i < len; i++) {
                ids.add(jsonArray.getInteger(i));
            }
        }
        adRoleService.deleteByPrimaryKeys(ids);
        return JsonResponse.success();
    }

    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse update(HttpServletRequest request) {

        String roleName = request.getParameter("roleName");
        Integer roleType = Integer.parseInt(request.getParameter("roleType"));
        Integer roleId = Integer.parseInt(request.getParameter("roleId"));

        AdRole adRole = new AdRole();
        adRole.setRoleId(roleId);
        adRole.setRoleName(roleName);
        adRole.setRoleType(roleType);


        adRoleService.updateByPrimaryKeySelective(adRole);
        adRole = adRoleService.selectByPrimaryKey(roleId);

        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adRole));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

    @RequestMapping(value = "/query", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse query(HttpServletRequest request) {
        List<AdRole> roleList = adRoleService.selectAdRoles();

        JsonResponse jsonResponse = new JsonResponse();
        Map<String, Object> map = new HashMap<>();

        map.put("items", NameCastTools.listToListMap(roleList));
        map.put("count", roleList.size());

        jsonResponse.setData(map);
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

}
